package projet.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;

import projet.dao.SessionDAOInterface;
import projet.entity.Client;
import projet.entity.Course;
import projet.entity.Location;
import projet.entity.Session;

public class JPASessionDAO implements SessionDAOInterface {

	@Override
	public List<Session> getList() {
		List<Session> sessions = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Session> query = em.createNamedQuery("Session.findAll", Session.class);
			sessions = query.getResultList();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return sessions;
	}

	@Override
	public Session getById(Long id) {
		Session session = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			session = em.find(Session.class, id);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return session;
	}

	@Override
	public void create(Session session) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			em.persist(session);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

	}

	@Override
	public void update(Session session) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			em.merge(session);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public void delete(Session session) {
		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			Session s = em.find(Session.class, session.getId());
			em.remove(s);

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}
	}

	@Override
	public Course getCourse(Long id) {
		Course course = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Course> query = em.createNamedQuery("Session.getCourse", Course.class);
			query.setParameter("id", id);
			course = query.getSingleResult();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return course;
	}

	@Override
	public Location getLocation(Long id) {
		Location location = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Location> query = em.createNamedQuery("Session.getLocation", Location.class);
			query.setParameter("id", id);
			location = query.getSingleResult();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return location;
	}

	@Override
	public List<Client> getClients(Long id) {
		List<Client> clients = null;

		EntityManager em = JPAUtil.getEntityManagerFactory().createEntityManager();
		try {
			em.getTransaction().begin();

			TypedQuery<Client> query = em.createNamedQuery("Session.findClients", Client.class);
			query.setParameter("id", id);
			clients = query.getResultList();

			em.getTransaction().commit();
		} catch (Exception e) {
			e.printStackTrace();
			try { em.getTransaction().rollback(); } catch (Exception e2) { e2.printStackTrace(); }
		} finally {
			try { em.close(); } catch (Exception e2) { e2.printStackTrace(); }
		}

		return clients;
	}

}
